define(["jquery","bbcare","comm/form/form.attention","comm/form/form.validate"],function($,$bb,$attention,$validate){
	var quest = {};

	/**
	 * tpl模板
	 */
	quest.tpl = function(){
		return 	'<script id="tpl_question_text" type="text-html">'+
				'	<div {{if relationId}}relationId="{{relationId}}"{{/if}} class="qaCard-text qaCard form-group form-group-sm col-sm-{{length}} col-md-{{length}} grid-label-{{labelLength}}">'+
				'		<label class="control-label {{align}}">{{label}}</label>'+
				'   	<div class="control-input">'+
				'			{{if status=="default"}}'+
				'   			<input type="text" class="form-control validatebox" name="{{name}}" data-type="{{type}}" data-innDeCode="{{innDeCode}}" data-deCode="{{deCode}}" data-dataUnit="{{dataUnit}}" placeholder="{{placeHolder}}" data-validate="{{validate}}" data-attention="{{attention}}" autocomplete="off">'+
				'			{{else if status=="readonly"}}'+
				'				<p class="form-control-static validatebox" name="{{name}}" data-type="{{type}}" data-innDeCode="{{innDeCode}}" data-deCode="{{deCode}}" data-dataUnit="{{dataUnit}}" data-validate="{{validate}}" data-attention="{{attention}}"></p>'+
				'			{{else if status=="disabled"}}'+
				'   			<input type="text" class="form-control validatebox" name="{{name}}" data-type="{{type}}" data-innDeCode="{{innDeCode}}" data-deCode="{{deCode}}" data-dataUnit="{{dataUnit}}" placeholder="{{placeHolder}}" data-validate="{{validate}}" data-attention="{{attention}}" disabled>'+
				'			{{/if}}'+
				'		</div>'+
				'	</div>'+
				'</script>';
	}

	/**
	 * 赋值
	 */
	quest.setValue = function($dom,value){
		if( value ){
			if( value.quesValue && value.quesValue == "doctorName"){
				var doctorName = JSON.parse($bb.getSessionStorage("account")).alias;
				value.quesValue = doctorName;
			}
			$dom.hasClass("form-control-static") ? $dom.html(value.quesValue) : $dom.val(value.quesValue);
		}
	}

	/**
	 * 获取value（保存时，获取每个dom对应的答案）
	 */
	quest.getValue = function($dom,name){
		var value = {};
		value.quesValue = $dom.hasClass("form-control-static") ? $dom.html() : $dom.val();
		value.quesId = $dom.attr("data-quesId");
		value.deCode = $dom.attr("data-deCode");
		value.innDeCode = $dom.attr("data-innDeCode");
		var dataUnit = $dom.attr("data-dataUnit");
		if( dataUnit ) value.dataUnit = dataUnit;
		return value;
	}

	/**
	 * 事件函数
	 */
	quest.event = function(params,id){
		var heightName = $bb.code.getDeCodeNameByName("height");
		var weightName = $bb.code.getDeCodeNameByName("weight");
		var $height = $("#" + id).find("input[type=text][name='" + heightName + "']");
		var $weight = $("#" + id).find("input[type=text][name='" + weightName + "']");
		if( $height.length == 1 && $weight.length == 1 ){
			$height.unbind("blur");
			$height.data("data",{"heightDom":$height,"weightDom":$weight,"id":id});
			$height.bind("blur",onInput);
			$weight.unbind("blur");
			$weight.data("data",{"heightDom":$height,"weightDom":$weight,"id":id});
			$weight.bind("blur",onInput);
		}
	}

	/**
	 * 提醒
	 */
	quest.attention = function(id){
        $("#" + id + " .qaCard-text .validatebox").bind("blur input propertychange",onAttention);
        // 初始化
        $("#" + id + " .qaCard-text .validatebox").blur();
	}

	/**
	 * 校验
	 * flag:
	 * 	 true:  (点击保存按钮时使用)
	 * 	 false: 
	 */
	quest.validate = function(id,flag){
        $("#" + id + " .qaCard-text .validatebox").bind("blur",onValidate);
        if( flag ){
        	var result = true;
        	$("#" + id + " .qaCard-text .validatebox").each(function(i){
        		var params = {
		            "value":getValue($(this)),
		            "dom":$(this),
		            "flag":flag,
		        };
				var success = $validate.init(params,handleByRuleValidate,handleBytypeValidate);
	            result = result && success;
	            if( !result ) return result;
	    	});
        	return result;
        }
	}

	/******************** event begin ********************/

	/**
	 * 身高、体重改变时触发
	 */
	var onInput = function(){
		var data = $(this).data("data");
		var $height = data.heightDom;
		var $weight = data.weightDom;
		var h_dataUnit = $height.attr("data-dataUnit");
		var w_dataUnit = $weight.attr("data-dataUnit");
		var h_val = $height.val();
		var w_val = $weight.val();
		var bmi = $bb.code.getDeCodeNameByName("bmi");
		if( h_val && w_val ){
			if( h_dataUnit == "cm" ) h_val = h_val / 100;
			if( w_dataUnit == "g" ) w_val = w_val / 1000;
			var val = ( w_val / h_val / h_val ).toFixed(2);
			$("#" + data.id).find("input[type=text][name='" + bmi + "']").length > 0 ? $("#" + data.id).find("input[type=text][name='" + bmi + "']").val(val) : $("#" + data.id).find("p[name='" + bmi + "']").html(val);
		}else{
			$("#" + data.id).find("input[type=text][name='" + bmi + "']").length > 0 ? $("#" + data.id).find("input[type=text][name='" + bmi + "']").val("") : $("#" + data.id).find("p[name='" + bmi + "']").html("");
		}
	}

	/******************** event end ********************/

	/**
	 * 获取输入的值(validate、attention中需要用到)
	 */
	var getValue = function($dom){
		var value = $dom.hasClass("form-control-static") ? $dom.html() : $dom.val();
		return value.trim();
	}

	/********************  attention begin  ********************/

	/**
	 * xx情况下触发的事件 (有可能是失去焦点等)
	 */
	var onAttention = function(){
		var params = {
            "value":getValue($(this)),
            "dom":$(this),
        };
		$attention.init(params,handleByRule,handleBytype);
    }

    /**
     * 根据单个规则rule获取单个规则的结果result
     * 该题型已有规则在case里拦截，公用规则从default里返回结果
     * @return Boolean [布尔值]
     */
    var handleByRule = function(obj){
    	var result;
    	switch(obj.type) {
            default:
            	result = $attention.handleByRule(obj);
        }
        return result;
    }

    /**
     * 根据提醒类型(type)执行对应的方法
     * 该题型已有提醒类型在case里拦截，公用提醒类型在default里处理
     */
    var handleBytype = function(obj){
    	if( obj.type ){
    		switch(obj.type) {
	            default:
            		$attention.handleBytype(obj);
	        }
    	}
    }

    /********************  attention end  ********************/

    /********************  validate begin  ********************/

    /**
	 * xx情况下触发的事件 (有可能是失去焦点等)
	 */
	var onValidate = function(){
		var params = {
            "value":getValue($(this)),
            "dom":$(this),
        };
		$validate.init(params,handleByRuleValidate,handleBytypeValidate);
	}

	/**
     * 根据单个规则rule获取单个规则的结果result
     * 该题型已有规则在case里拦截，公用规则从default里返回结果
     * @return Boolean [布尔值]
     */
    var handleByRuleValidate = function(obj){
    	var result = true;
    	switch(obj.type) {
            default:
            	result = $validate.handleByRule(obj);
        }
        return result;
    }

    /**
     * 根据提醒类型(type)执行对应的方法
     * 该题型已有提醒类型在case里拦截，公用提醒类型在default里处理
     */
    var handleBytypeValidate = function(obj){
    	if( obj.type ){
    		switch(obj.type) {
	            default:
            		$validate.handleBytype(obj);
	        }
    	}
    }

	/********************  validate begin  ********************/

	return quest;
});